package dao.impl;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import util.db.DBConn;
import dao.SuggestMagDao;
import domain.Suggest;

/**
 * 
 * May 14, 2009
 *
 * @author Fabian
 *
 */
public class SuggestMagImpl implements SuggestMagDao {

	/* (non-Javadoc)
	 * @see dao.SuggestMagDao#addSuggest(vo.Suggest)
	 */
	
	public boolean addSuggest(Suggest s) {
		boolean result = false;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "insert into t_suggest values (null, ?, ?, ?, ?, now(), now(), ?, 0)";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setString(1, s.getTitle());
			pstmt.setString(2, s.getContent());
			pstmt.setInt(3, s.getUser_id());
			pstmt.setInt(4, s.getRole_id());
			pstmt.setInt(5, s.getSort_id());
			int rsColum = pstmt.executeUpdate();		
			if (rsColum > 0) {
				result = true;
				return result;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return result;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return result;
	}

	/* (non-Javadoc)
	 * @see dao.SuggestMagDao#delSuggest(int)
	 */
	
	public boolean delSuggest(int id) {
		boolean result = false;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "delete from t_suggest where id = ?";
		try {
			conn = DBConn.getConn();			
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setInt(1, id);
			int rsColum = pstmt.executeUpdate();		
			if (rsColum > 0) {
				result = true;
				return result;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return result;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		
		return result;
	}

	/* (non-Javadoc)
	 * @see dao.SuggestMagDao#getPageStudent(int, int)
	 */
	
	public List<Suggest> getPageSuggest(int start, int limit) {
		
		List<Suggest> list = new ArrayList<Suggest>();		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		ResultSet rs2 = null;
		String sql = "select * from t_suggest order by publish_time desc limit ?, ?";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			pstmt.setInt(1, start);
			pstmt.setInt(2, limit);
			rs = pstmt.executeQuery();			
			while (rs.next()) {
				Suggest s = new Suggest();
				s.setId(rs.getInt("id"));
				s.setTitle(rs.getString("title"));
				s.setContent(rs.getString("content"));
				s.setPubDate(rs.getTimestamp("publish_time"));
				int bug_advice = rs.getInt("bug_advice");
				if (bug_advice == 1) {
					s.setSort("建议");
				} else if (bug_advice == 0) {
					s.setSort("BUG");
				}
				int user_id = rs.getInt("user_id");
				int roleId = rs.getInt("role");				
				if (roleId == 3) {		//如果是学生
					s.setRole("学生");
					String sql3 = "select name from t_student where id = ?"; 
					pstmt = DBConn.getPstmt(conn, sql3);
					pstmt.setInt(1, user_id);
					rs2 = pstmt.executeQuery();	
					if (rs2.next()) {
						s.setAuthor(rs2.getString(1));
					}
					rs2.close();
				} else if (roleId == 2) {			//如果是老师
					s.setRole("老师");
					String sql2 = "select name from t_teacher where id = ?"; 
					pstmt = DBConn.getPstmt(conn, sql2);
					pstmt.setInt(1, user_id);
					rs2 = pstmt.executeQuery();	
					if (rs2.next()) {
						s.setAuthor(rs2.getString(1));
					}
					rs2.close();
				} else if (roleId == 1) {			//如果是管理员
					s.setRole("管理员");
					String sql1 = "select name from t_admin where id = ?"; 
					pstmt = DBConn.getPstmt(conn, sql1);
					pstmt.setInt(1, user_id);
					rs2 = pstmt.executeQuery();	
					if (rs2.next()) {
						s.setAuthor(rs2.getString(1));
					}
					rs2.close();
				}	
				list.add(s);
			}
			return list;
		} catch(SQLException e) {
			e.printStackTrace();
			return null;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
	}

	/* (non-Javadoc)
	 * @see dao.SuggestMagDao#getTotalCount()
	 */
	
	public int getTotalCount() {
		int totalCount = 0;
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		String sql = "select count(*) from t_suggest";
		try {
			conn = DBConn.getConn();
			pstmt = DBConn.getPstmt(conn, sql);
			rs = pstmt.executeQuery();			
			if (rs.next()) {
				totalCount = rs.getInt(1);
				return totalCount;
			}
		} catch(SQLException e) {
			e.printStackTrace();
			return totalCount;
		} finally {
			DBConn.close(conn);
			DBConn.close(pstmt);
			DBConn.close(rs);
		}
		return 0;
	}

}
